package com.briup.cms.web.controller;

import com.briup.cms.bean.BaseUser;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.Date;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private IBaseUserService service;


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @ApiOperation("通过id删除")
    @ApiImplicitParam(name = "id",dataType = "int")
    @DeleteMapping("/delete")
    public Message delete(int id){
        return MessageUtil.success();
    }

    @ApiOperation("获取当前用户的基本信息")
    @ApiImplicitParam(name = "Authorization",value = "token值",paramType = "header")
    @GetMapping("/info")
    public Message info(@ApiIgnore @RequestHeader("Authorization")  String Authorization){
        return MessageUtil.success(service.currentUserInfo(Authorization));
    }

    @ApiOperation("根据用户名角色、名分页查询用户信息")
    @ApiImplicitParams({
            //token 是保存在请求头中
            @ApiImplicitParam(name = "pageNum",value = "当前页",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",dataType = "int"),
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String"),
            @ApiImplicitParam(name = "role",value = "角色名,0-admin,1-user", allowableValues = "0,1",dataType = "Integer")
    })
    @GetMapping("/page/query")
    public Message pageQuery(int pageNum,int pageSize,String username,Integer role){
//        service.pageQuery(pageNum,pageSize,username,role);
        return MessageUtil.success(service.pageQuery(pageNum,pageSize,username,role));
    }

    @ApiOperation("更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "Integer"),
            @ApiImplicitParam(name = "realname",value = "真实姓名",dataType = "String"),
            @ApiImplicitParam(name = "telephone",value = "电话",dataType = "String"),
            @ApiImplicitParam(name = "gender",value = "性别,0-男,1-女", allowableValues = "0,1",dataType = "Integer"),
            @ApiImplicitParam(name = "dob",value = "出生日期",dataType = "Date"),
            @ApiImplicitParam(name = "email",value = "电子邮箱",dataType = "String")
    })
    @PutMapping("/update")
    public Message update(Integer userId, String realname, String telephone, Integer gender, Date dob, String email){

        BaseUser user = new BaseUser();
        user.setUserId(userId);
        user.setRealname(realname);
        user.setTelephone(telephone);
        user.setGender(gender);
        System.out.println("dob打印"+dob);
        user.setDob(dob);
        user.setEmail(email);

        service.updateUser(user);
        return MessageUtil.success();
    }

    @ApiOperation("修改用户头像")
    @ApiImplicitParams({
            //token 是保存在请求头中
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "Integer"),
            @ApiImplicitParam(name = "userIcon",value = "图像url",dataType = "String")
    })
    @PutMapping("/updateUserIcon")
    public Message updateUserIcon(Integer id,String userIcon){
        service.updateUserIcon(id,userIcon);
        return MessageUtil.success(userIcon);
    }







}
